package com.devplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.devplatform.pojo.Fans;
import com.devplatform.pojo.dto.FollowDto;
import com.devplatform.pojo.vo.DataVo;
import com.devplatform.service.FansService;
import com.devplatform.util.UserContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/follow")
@RequiredArgsConstructor

public class FollowController {
    private final FansService fansService;

    //关注/取消关注接口
    @PostMapping("/toggle")
    public DataVo toggleFollow(@RequestBody FollowDto authorDto) {
        Long fansId = UserContextHolder.getUserId();
        return fansService.toggleFollow(fansId, authorDto);
    }

    //查询关注的粉丝数
    @GetMapping("/getFansCount")
    public DataVo getFansCount(@RequestParam Long userId) {
        return fansService.getFansCount(userId);
    }

    //查询关注列表
    @GetMapping("/getFansList")
    public DataVo getFansList(@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int size,
                              @RequestParam Long userId) {
        IPage<Fans> fansIPage = new Page<>(page, size);
        return fansService.getFansList(fansIPage, userId);
    }

    //获取关注列表
    @GetMapping("/getFollowingList")
    public DataVo getFollowingList(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int size) {
        Long userId = UserContextHolder.getUserId();
        IPage<Fans> fansIPage = new Page<>(page, size);
        return fansService.getFollowingList(fansIPage, userId);
    }

    //统计粉丝数据
    @GetMapping("/getFansStatistics")
    public DataVo getFansStatistics(@RequestParam Long userId) {
        return fansService.getFansStatistics(userId);
    }

    //数据分析
    @GetMapping("/getFansAnalysis")
    public DataVo getFansAnalysis() {
        Long userId = UserContextHolder.getUserId();
        return fansService.getFansAnalysis(userId);
    }

    //是否已经关注
    @GetMapping("/checkFollowing/{authorId}")
    public DataVo<Boolean> checkFollowing(@PathVariable Long authorId) {
        Long fansId = UserContextHolder.getUserId();
        boolean isFollowing = fansService.isFollowing(fansId, authorId);
        return DataVo.ok(isFollowing);
    }

    //搜索粉丝
    @GetMapping("/searchFans")
    public DataVo searchFans(@RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "10") int size,
                             @RequestParam Long userId,
                             @RequestParam(required = false) String keyword) {
        IPage<Fans> fansIPage = new Page<>(page, size);
        return fansService.searchFans(fansIPage, userId, keyword);
    }

    // 搜索关注
    @GetMapping("/searchFollowing")
    public DataVo searchFollowing(@RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int size,
                                  @RequestParam(required = false) String keyword) {
        Long userId = UserContextHolder.getUserId();
        IPage<Fans> fansIPage = new Page<>(page, size);
        return fansService.searchFollowing(fansIPage, userId, keyword);
    }
}
